package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.TaskDetailsListBean;
import com.store.app.bean.TaskHeadBean;
import com.store.app.bean.TaskLogBean;

import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */

public class TaskWillSubmitDetialsListAdapter extends BaseAdapter {
    private List<TaskDetailsListBean> list;
    private Context context;
    private OnClickOrder onClickOrder;
    final int TYPE_1 = 0;       //加载头布局view
    final int TYPE_2 = 1;       //加载尾布局view
    final int TYPE_3 = 2;       //加载正常item布局
    int tvlinecount=-1;          //行数
    boolean  iszhangkai=false;  //是否展开
    private TaskHeadBean taskHeadBean;
    private List<TaskLogBean> logBeanList;
    private Handler handler = new Handler();
    public interface OnClickOrder {
        void changePhoto(TaskDetailsListBean bean, int index);//取消
    }
    public void setTaskHeadBean(TaskHeadBean taskHeadBean){
        this.taskHeadBean = taskHeadBean;
    }
    public TaskWillSubmitDetialsListAdapter(Context context, List<TaskDetailsListBean> list,List<TaskLogBean> logBeanList, OnClickOrder onClickOrder) {
        this.context = context;
        this.list = list;
        this.logBeanList = logBeanList;
        this.onClickOrder = onClickOrder;
    }

    @Override
    public int getCount() {
        return list.size()+1+logBeanList.size();
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getItemViewType(int position) {
        if(position==0) {
            return TYPE_1;
        }else if (position>0&& position<=(list.size())){
            return TYPE_3;
        }else{
            return TYPE_2;
        }

    }
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 3;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        int viewtype = getItemViewType(position);
        //加载头布局
        if(TYPE_1 == viewtype){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_task_head, null);
            TextView tv_content_title = (TextView) convertView.findViewById(R.id.tv_task_title);
            final TextView tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            TextView tv_reward = (TextView) convertView.findViewById(R.id.tv_reward);
            TextView tv_money_type = (TextView) convertView.findViewById(R.id.item_task_money_tpe);
            final ImageView img_arrow=(ImageView) convertView.findViewById(R.id.img_arrow);
            TextView tv_expiration_date = (TextView) convertView.findViewById(R.id.tv_expiration_date);
            if(taskHeadBean!=null) {
                tv_content_title.setText(taskHeadBean.getTv_content_title());
                tv_content.setText(taskHeadBean.getTv_content());

                if("gold".equals(taskHeadBean.getPayment_way_key())) {
                    tv_reward.setText(taskHeadBean.getTv_reward());
                    tv_money_type.setText("金币");
                }else{
                    tv_reward.setText(taskHeadBean.getTv_reward());
                    tv_money_type.setText("元");
                }
                tv_expiration_date.setText(taskHeadBean.getTv_expiration_date());
//                if(tvlinecount==-1) {
//                    try {
//                        //根据高度来判断是否需要再点击展开
//                        handler.post(new Runnable() {
//                            @Override
//                            public void run() {
//                                Log.i("mylog", "aa" + tv_content.getLineCount());
//                                tvlinecount = tv_content.getLineCount();
//                                if (tvlinecount <= 4) {
//                                    img_arrow.setVisibility(View.GONE);
//                                } else {
//                                    img_arrow.setVisibility(View.VISIBLE);
//                                    tv_content.setMaxLines(4);
//                                }
//                            }
//                        });
//                    }catch (Exception e){
//
//                    }
//                }else{
//                    if (tvlinecount <= 4) {
//                        img_arrow.setVisibility(View.GONE);
//                    } else {
//                        img_arrow.setVisibility(View.VISIBLE);
//                        if(!iszhangkai){
//                            img_arrow.setImageResource(R.drawable.task_arrow_down);
//                            tv_content.setMaxLines(4);
//                        }else{
//                            img_arrow.setImageResource(R.drawable.task_arrow_up);
//                            tv_content.setMaxLines(100);
//                        }
//                    }
//                }
//                img_arrow.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Log.i("mylog", tvlinecount + "  " + iszhangkai);
//                        if(iszhangkai){
//                            iszhangkai=false;
//                        }else{
//                            iszhangkai=true;
//                        }
//                        notifyDataSetChanged();
//                    }
//                });

            }


        }
        //加载尾布局
        else if(TYPE_2 ==viewtype){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_view, null);
            TextView time = (TextView) convertView.findViewById(R.id.show_time);
            TextView title = (TextView) convertView.findViewById(R.id.show_title);
            TextView image = (TextView) convertView.findViewById(R.id.image);
            LinearLayout ll_top = (LinearLayout) convertView.findViewById(R.id.ll_top);
            View line = convertView.findViewById(R.id.line_normal);
            LinearLayout llContent = (LinearLayout) convertView.findViewById(R.id.ll_content);



            if(position==(list.size()+1)){
                ll_top.setVisibility(View.VISIBLE);
                image.setBackgroundResource(R.drawable.pic_point_open);
            }else{
                ll_top.setVisibility(View.GONE);
                image.setBackgroundResource(R.drawable.pic_point_close);
            }
            time.setText(logBeanList.get(position-(list.size()+1)).getTime());
            title.setText(logBeanList.get(position-(list.size()+1)).getTitle());

            // 最后一项时，竖线不再显示
            if (position == (logBeanList.size()+list.size())) {
                line.setVisibility(View.GONE);
            }else{
                line.setVisibility(View.VISIBLE);
            }

//            Log.v("zyl","TYPE_2TYPE_2TYPE_2TYPE_2TYPE_2TYPE_2");
//            convertView = LayoutInflater.from(context).inflate(R.layout.item_task_bottom, null);
//            ListView lv = (ListView) convertView.findViewById(R.id.lv_log);
//            TaskLogAdapter logAdapter = new TaskLogAdapter(context,logBeanList);
//            lv.setAdapter(logAdapter);
//            Utils.Setlistview(lv);
        }
        else if(TYPE_3 == viewtype){

            final TaskDetailsListBean bean = list.get(position-1);

            convertView = LayoutInflater.from(context).inflate(R.layout.item_task_will_submit_mode, null);
            TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            ImageView iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);
            RelativeLayout rl_zhezhao = (RelativeLayout) convertView.findViewById(R.id.rl_zhezhao);
            //先判断是否有图片地址,如果有，则加载图片，并且显示遮罩层。
            tv_title.setText(bean.getTitle());
            if (bean.getBitmap() != null) {
                iv_picture.setImageBitmap(bean.getBitmap());
                rl_zhezhao.setVisibility(View.VISIBLE);
            } else if (!TextUtils.isEmpty(bean.getPicUrl())) {
                ImageLoader.getInstance().displayImage(bean.getPicUrl(), iv_picture, options);
                rl_zhezhao.setVisibility(View.VISIBLE);
            } else {
                rl_zhezhao.setVisibility(View.GONE);
            }
            iv_picture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickOrder.changePhoto(bean, position-1);
                }
            });
        }

        return convertView;
    }


    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.pic_icon_big_default)
            .showImageOnFail(R.drawable.pic_icon_big_default)
            .showStubImage(R.drawable.pic_icon_big_default)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
